package com.karst.essentials.chapter07.galaxyExample
{
	import org.papervision3d.core.proto.GeometryObject3D;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.PaperPlane;
	
	internal class Player extends DisplayObject3D
	{
		public function Player()
		{
			super();
			createView() ;
		}

		private var light : PointLight3D = null ;
		private var paperPlane : PaperPlane = null ;
		private var shadeMaterial : FlatShadeMaterial = null ;
		private function createView() : void
		{
			if( light == null )
				light = new PointLight3D() ;
			if( shadeMaterial == null )
			{
				shadeMaterial = new FlatShadeMaterial( light, 0xFFFFFF, 0x848484) ;
				shadeMaterial.doubleSided = true ;
			}
			if( paperPlane == null )
			{
				paperPlane = new PaperPlane( shadeMaterial, 2 ) ;
				paperPlane.localRotationX = 10 ;
			}
			addChild( paperPlane ) ;
			y = 100 ;
			z = -1000 ;
		}
		
		private var topSpeed : Number = 0 ;
		private var speed : Number = 0 ;
		
		private var topSteer : Number = 0 ;
		private var steer : Number = 0 ;
		public function update() : void
		{
			if( UserInputHandler.keyForward )
			{
				topSpeed = 30 ;
			}
			else if( UserInputHandler.keyBackward )
			{
				topSpeed = -20 ;
			}
			else
			{
				topSpeed = 0 ;
			}
			speed -= ( speed - topSpeed ) * .1 ;
			moveForward( speed ) ;
			
			
			
			if( UserInputHandler.keyRight )
			{
				topSteer = Math.max( topSteer, 30 ) ;
			}
			else if( UserInputHandler.keyLeft )
			{
				topSteer = Math.min( topSteer, -30 ) ;
			}
			else
			{
				topSteer -= topSteer * .1 ;
			}
			steer -= ( steer - topSteer ) * .1 ;
			yaw( speed * steer * .002 ) ;
			paperPlane.localRotationZ = steer ;
		}
	}
}